//
//  DetailsViewController.m
//  HorizontalAnimationDemo
//
//  Created by mysj on 16/7/6.
//  Copyright © 2016年 __defaultyuan. All rights reserved.
//

#import "DetailsViewController.h"
#import "PhotoBrowserLayout.h"
#import "PhotoBrowserCell.h"
#import "Masonry.h"
#import <QuartzCore/QuartzCore.h>
#import <AssetsLibrary/AssetsLibrary.h>



static NSString *const photoBrowsercellId = @"photoBrowsercellId";

@interface DetailsViewController () <UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UIView *           backView;
@property (nonatomic, strong) UIImageView *      backImgView;
@property (nonatomic, strong) UILabel *          titleLabel;
@property (nonatomic, strong) UILabel *          detailLabel;
@property (nonatomic, strong) UIButton *         disButton;
@property (nonatomic, strong) UICollectionView * photoBrowser;
@property (nonatomic, strong) UIButton *         certainButton;
@property (nonatomic, strong) UIButton *         cancelButton;

@property (nonatomic, strong) NSArray *          assets;
@property (nonatomic, assign) NSInteger          cellCount;
@property (nonatomic, strong) NSCache *          imageCache;
@end

@implementation DetailsViewController


- (void)show {
    [UIView animateWithDuration:0.35 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0.8 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    
        
        CGRect newFrame = self.view.frame;
        newFrame.origin.y = [UIScreen mainScreen].bounds.size.height - self.view.frame.size.height;
        self.view.frame = newFrame;
    } completion:nil];

}
#pragma mark- View cycle

- (void)loadView {
    [super loadView];
    
    [self prepareSubviews];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imageCache = [[NSCache alloc] init];
    
    NSMutableArray *theAssets = [NSMutableArray array];
    
    NSURL *theURL = [[[NSBundle mainBundle] resourceURL] URLByAppendingPathComponent:@"Images"];
    
    NSEnumerator *theEnumerator = [[NSFileManager defaultManager] enumeratorAtURL:theURL includingPropertiesForKeys:NULL options:NSDirectoryEnumerationSkipsPackageDescendants | NSDirectoryEnumerationSkipsHiddenFiles errorHandler:NULL];
    
    for (theURL in theEnumerator)
    {
        if ([[theURL pathExtension] isEqualToString:@"jpg"])
        {
            [theAssets addObject:theURL];
        }
    }
    
    self.assets = theAssets;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self dismiss];
}
#pragma mark- API

- (void)dismiss {
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark-


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.assets.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PhotoBrowserCell *theCell = [collectionView dequeueReusableCellWithReuseIdentifier:photoBrowsercellId forIndexPath:indexPath];
    if (theCell.gestureRecognizers.count == 0) {
        [theCell addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCell:)]];
    }
    
    if (indexPath.row < self.assets.count) {
        
        NSURL *theURL = [self.assets objectAtIndex:indexPath.row];
        UIImage *theImage = [self.imageCache objectForKey:theURL];
        if (theImage == NULL) {
            theImage = [UIImage imageWithContentsOfFile:theURL.path];
            
            [self.imageCache setObject:theImage forKey:theURL];
        }
        
        theCell.imageView.image = theImage;
        theCell.backgroundColor = [UIColor clearColor];
    }
    
    
    return theCell;
}


- (void)tapCell:(UITapGestureRecognizer *)inGestureRecognizer {
    
    NSIndexPath *theIndexPath = [self.photoBrowser indexPathForCell:(UICollectionViewCell *)inGestureRecognizer.view];
    NSLog(@"%@", theIndexPath);
//    NSLog(@"%@", [self.photoBrowser.collectionViewLayout layoutAttributesForItemAtIndexPath:theIndexPath]);
//    NSURL *theURL = [self.assets objectAtIndex:theIndexPath.row];
//    NSLog(@"%@", theURL);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    [self updateTitle];
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

#pragma mark - prepare

- (void)prepareSubviews {
//    self.view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.backView];
    [_backView addSubview:self.backImgView];
    [_backView addSubview:self.titleLabel];
    [_backView addSubview:self.detailLabel];
    [_backView addSubview:self.disButton];
    [self.view addSubview:self.photoBrowser];
    [self.view addSubview:self.cancelButton];
    [self.view addSubview:self.certainButton];
    
    
    
    [self.backView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.bottom.equalTo(self.view).offset(-120);
        make.height.equalTo(@200);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backView).offset(15);
        make.right.equalTo(_backView).offset(-15);
        make.top.equalTo(_backView).offset(10);
    }];
    
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backView).offset(15);
        make.right.equalTo(_backView).offset(-15);
        make.top.equalTo(_titleLabel.mas_bottom).offset(5);
    }];
    
    [self.disButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_backView.mas_top).offset(5);
        make.right.equalTo(_backView.mas_right).offset(-10);
    }];
    
    [self.photoBrowser mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_detailLabel.mas_bottom).offset(15);
        make.left.equalTo(_backView).offset(15);
        make.right.equalTo(_backView).offset(-15);
        make.height.equalTo(@125);
    }];
    
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backView).offset(5);
        make.top.equalTo(_backView.mas_bottom).offset(20);
    }];
    
    [self.certainButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_backView).offset(-5);
        make.top.equalTo(_backView.mas_bottom).offset(20);
    }];
    
}

- (UIButton *)cancelButton {
    if (_cancelButton == nil) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelButton setImage:[UIImage imageNamed:@"取消_29"] forState:UIControlStateNormal];
    }
    return _cancelButton;
}

- (UIButton *)certainButton {
    if (_certainButton == nil) {
        _certainButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_certainButton setImage:[UIImage imageNamed:@"取消_29"] forState:UIControlStateNormal];
    }
    return _certainButton;
}

- (UICollectionView *)photoBrowser {
    
    if (_photoBrowser == nil) {
        
        PhotoBrowserLayout *layout = [[PhotoBrowserLayout alloc] init];
        CGRect frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 250);
        _photoBrowser = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
        
        _photoBrowser.dataSource = self;
        _photoBrowser.delegate = self;
        _photoBrowser.showsHorizontalScrollIndicator = NO;
        _photoBrowser.backgroundColor = [UIColor clearColor];
        [_photoBrowser registerClass:[PhotoBrowserCell class] forCellWithReuseIdentifier:photoBrowsercellId];
    }
    return _photoBrowser;
}

- (UIButton *)disButton {
    if (_disButton == nil) {
        _disButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_disButton setImage:[UIImage imageNamed:@"ia_message_delect"] forState:UIControlStateNormal];
        [_disButton setImage:[UIImage imageNamed:@"ia_message_delect"] forState:UIControlStateHighlighted];
    }
    return _disButton;
}

- (UILabel *)detailLabel {
    if (_detailLabel == nil) {
        // #a6f8ff
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.textColor = [UIColor blueColor];
        _detailLabel.font = [UIFont systemFontOfSize:9];
        _detailLabel.text = @"lasdka;sldka;lskd;lakss;alkd;l";
        _detailLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _detailLabel;
}

- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor blueColor];
        _titleLabel.font = [UIFont systemFontOfSize:12];
        _titleLabel.text = @"长新大厦";
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UIImageView *)backImgView {
    if (_backImgView == nil) {
        _backImgView = [[UIImageView alloc] init];
    }
    return _backImgView;
}

- (UIView *)backView {
    
    if (_backView == nil) {
        _backView = [[UIView alloc] init];
//        _backView.backgroundColor = [UIColor whiteColor];
    }
    return _backView;
}

@end
